# -*- coding = utf-8 -*-
# @Time : 2020/8/3 17:17
# @Author : 姚远
# @File : testurllib.py
# @Software: PyCharm

import urllib.parse
import urllib.request

# 获取一个get请求
response = urllib.request.urlopen("http://www.baidu.com")
print(response.read().decode('utf-8'))


# 获取一个post请求
data = bytes(urllib.parse.urlencode({'hello': 'word'}), encoding='utf-8')
response = urllib.request.urlopen("http://httpbin.org/post", data=data)
print(response.read().decode('utf-8'))


# 只是为了测试不出现Error 418：
url = "http://httpbin.org/post"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/84.0.4147.105 Safari/537.36 "
}
data = bytes(urllib.parse.urlencode({'hello': 'word'}), encoding='utf-8')
req = urllib.request.Request(url=url, data=data, headers=headers, method='POST')
response = urllib.request.urlopen(req)
print(response.read().decode('utf-8'))


# 测试爬豆瓣
url = "http://movie.douban.com/top250?start=0"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/84.0.4147.105 Safari/537.36 "
}
req = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(req)
print(response.read().decode('utf-8'))